
<script type="text/javascript" src="http://prototypejs.org/assets/2009/8/31/prototype.js" ></script>

<script type="text/javascript">
;
Element.addMethods({  
	  getInnerText: function(element) {
	    element = $(element);
	    return element.innerText && !window.opera ? element.innerText
	      : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
	  }
	});


var Company = Class.create({
  initialize: function() {
  },
  
  getWorkers: function() { // zwraca tablice pracownikow
    return this.workers;
  },

  setWorkers: function(arg) {
	    this.workers = arg;
  },
	
  getRawRequired: function() { // zwraca tablice pracownikow
    return this.rawRequired;
  },
  setRawRequired: function(arg) {
	    this.rawRequired = arg;
  },

  	getUnitsProduced: function() {
	    return this.unitsProduced;
	},
	setUnitsProduced: function(arg) {
	  this.unitsProduced = arg;
	},

  	getSales: function() {
	    return this.sales;
	},
	setSales: function(arg) {
	  this.sales = arg;
	},

  	getRawUsed: function() {
	    return this.rawUsed;
	},
	setRawUsed: function(arg) {
	  this.rawUsed = arg;
	},

  	getProductPrice: function() {
	    return this.productPrice;
	},
	setProductPrice: function(arg) {
	  this.productPrice = arg;
	},


  	getRawPrice: function() {
	    return this.rawPrice;
	},
	setRawPrice: function(arg) {
	  this.rawPrice = arg;
	},


  	getCurrencyValue: function() {
	    return this.currencyValue;
	},
	setCurrencyValue: function(arg) {
	  this.currencyValue = arg;
	},

  	getProfit: function() {
	    return this.profit;
	},
	setProfit: function(arg) {
	  this.profit = arg;
	},

  	getTotalSalaries: function() {
	    return this.totalSalaries;
	},
	setTotalSalaries: function(arg) {
	  this.totalSalaries = arg;
	},
	

  	getBoostersCost: function() {
	    return this.boostersCost;
	},
	setBoostersCost: function(arg) {
	  this.boostersCost = arg;
	}  
});



var Worker = Class.create({
	  initialize: function() {
	  },
	  
	  
	  
	  getWellness: function() {
	    return this.wellness;
	  },

	  setWellness: function(arg) {
	    this.wellness = arg;
	  },
	  
	  getSkill: function() {
	    return this.skill;
	  },
	  
	  setSkill: function(arg) {
	    this.skill = arg;
	  },

	  getSalary: function() {
		    return this.salary;
	  },
	  setSalary: function(arg) {
	    this.salary = arg;
	  },

	  getProfit: function() {
		    return this.profit;
	  },
	  
	  setProfit: function(arg) {
	    this.profit = arg;
	  },
	  
	  getProductivity: function() {
		    return this.productivity;
	  },
	  setProductivity: function(arg) {
	    this.productivity = arg;
	  },
	  
	  getDisabled: function() {
		    return this.checked;
	  },
	  
	  setDisabled: function(arg) {
	    this.checked = arg;
	  },

	  
	  getBooster: function() {
		    return this.boosted;
	  },
	  
	  setBooster: function(arg) {
	    this.boosted = arg;
	  },
	  
	  
	  	  
	  getBoosterCost: function() {
	  	var costInGold = 0;

		if (this.getBooster()==1.5)
			costInGold = 0.19;
		if (this.getBooster()==2.0)
			costInGold = 0.79;
		if (this.getBooster()==3.0)
			costInGold = 0.99;		
			
		var currencyForGold = 1.0/$("currencyValue").value;
		return costInGold * currencyForGold;
	  },
	  
	  
	});


var newWorkersName = ["Mixliarder", "Uner", "Parter", "Battlegazi", "Vingaer", 
	"Graculka", "Kitowiec", "Grapox", "MISI", "Pabloo", "Blazz", "Sylwester zwierz", 
	"Carbon", "DejoT", "Aniakk", "Urbaneqqq", "admin"];


function getRandomWorkerName() {
	return newWorkersName[Math.floor(Math.random()*newWorkersName.length)]
}


function addWorker() {
	
	var workersNumber=0;
	while ($("worker[" + workersNumber + "]")) {
		workersNumber++;
	}
	
	var newWorkerCode =
			"<td id=\"workerID["+workersNumber+"]\"> "+(workersNumber+1)+"  </td>" +
			"<td> <a href=\"http://www.erepublik.com/en/citizen/profile/1\" target=\"_blank\"> "+getRandomWorkerName()+" </a> </td>  " +
			"<td><input id=\"workerSkill["+workersNumber+"]\" type=\"text\" size=\"2\" value=\"1\"/> </td> " +
			"<td><input id=\"workerWellness["+workersNumber+"]\" type=\"text\" size=\"4\" value=\"100\"/> </td> " +
			"<td><select id=\"workerBooster["+workersNumber+"]\">" +
				"<option value=\"1.1\">+10%(0g)</option>" +
				"<option value=\"1.5\">+50%(0.19g)</option>" +
				"<option value=\"2.0\">+100%(0.79g)</option>" +
				"<option value=\"3.0\">+200%(0.99g)</option>" +
			"</select></td>" +
			"<td id=\"workerProductivity["+workersNumber+"]\"> 0 </td> " +
			"<td id=\"workerUnitsProduced["+workersNumber+"]\"> 0 </td>" +
			"<td> <input id=\"workerSalary["+workersNumber+"]\" type=\"text\" size=\"4\" value=\"1\"/> " + getCurrencyName() + " </td> " +
			"<td id=\"workerProfit["+workersNumber+"]\" > 0" + getCurrencyName() + " </td> "+
			"<td><input id=\"workerDisable["+workersNumber+"]\" type=\"checkbox\" /></td>" +
			"<td><a id=\"workerDelete["+workersNumber+"]\" href=\"#\" onmousedown=\"deleteWorker("+workersNumber+");\"><img src=\"http://campus.codechef.com/images/cross-icon.gif\" border=\"0\"/></a></td>";
			
	var newNode = document.createElement("TR");
	newNode.setAttribute("id", "worker["+workersNumber+"]");
	newNode.insert( newWorkerCode );
	
	$("workersTable").appendChild(newNode);
			
	
}


function getWorkersFromForm() {
	var workers = [];

	var i=0;
	while ($("worker[" + i + "]")) {
		var worker = new Worker();
		worker.setSkill( $("workerSkill[" + i + "]").value );
		worker.setWellness( $("workerWellness[" + i + "]").value );
		worker.setSalary( $("workerSalary[" + i + "]").value );
		worker.setDisabled( $("workerDisable[" + i + "]").checked );
		worker.setBooster( $("workerBooster[" + i + "]").value );
		workers[i] = worker;
		i++;
	}

	return workers;
}


function isRaw() {
	return $("rawRegionLevel").getInnerText().search("n/a")==-1;
}


function deleteWorker(i) {
	
	
	var toBeRemoved = $("worker[" + i + "]");
	
	var temp = $("workerDelete[" + i + "]").onmousedown;
	i = i+1;
	while ($("worker[" + i + "]")) {
		
		var temp2=$("workerDelete[" + i + "]").onmousedown;
		$("workerDelete[" + i + "]").onmousedown = temp;
		temp = temp2;
		
		$("workerSkill[" + i + "]").id = "workerSkill[" + (i-1) + "]";
		$("workerWellness[" + i + "]").id = "workerWellness[" + (i-1) + "]";
		$("workerBooster[" + i + "]").id = "workerBooster[" + (i-1) + "]";
		$("workerProductivity[" + i + "]").id = "workerProductivity[" + (i-1) + "]";
		$("workerUnitsProduced[" + i + "]").id = "workerUnitsProduced[" + (i-1) + "]";
		$("workerSalary[" + i + "]").id = "workerSalary[" + (i-1) + "]";
		$("workerProfit[" + i + "]").id = "workerProfit[" + (i-1) + "]";		
		$("workerDisable[" + i + "]").id = "workerDisable[" + (i-1) + "]";	

		$("workerDelete[" + i + "]").id = "workerDelete[" + (i-1) + "]";	
		
		
		$("workerID[" + i + "]").innerHTML = i;
		$("workerID[" + i + "]").id = "workerID[" + (i-1) + "]";
		$("worker[" + i + "]").id = "worker[" + (i-1) + "]";
		i++;
	}
	
	toBeRemoved.remove();
	
}


function getWorkerNumberMultiplier(company) {
	var multiplier=1;
	var recommendedWorkers=10;
	if ( $("productTypeValue").getInnerText().search("House") >= 0)
		recommendedWorkers=20;
	workersNumber = company.getWorkers().length;

    if (workersNumber <= recommendedWorkers)
        multiplier = 1 + (workersNumber / recommendedWorkers);
    if (workersNumber > recommendedWorkers)
        multiplier = 3 - (workersNumber / recommendedWorkers);

    if (multiplier < 1)
        multiplier = 1;

	return multiplier;
}

function getRawMultiplayer(company) {
	if ( $("rawRegionLevel").getInnerText().search("medium")>=0 )
		return 0.5;
	if ( $("rawRegionLevel").getInnerText().search("low")>=0 )
		return 0.01;	
		
	return 1.0;
}

function getSkillMultiplier(worker) {
	return 3.6 + worker.getSkill() * 0.4;
}

function getBoosterMultiplier(worker) {
	return worker.getBooster();
}

function getWellnessMultiplier(worker) {
    return 1 + 2 * worker.getWellness() / 100.0;
}

function updateGUI( company ) {
	var workers = company.getWorkers();
	for (i=0; i < workers.length; i++) {
		if (workers[i].getProfit()<=0) {
			$("workerProfit["+i+"]").setAttribute("class", "red");
		}
		else {
			$("workerProfit["+i+"]").setAttribute("class", "green");
		}
		$("workerProfit["+i+"]").innerHTML = workers[i].getProfit()+ " " + getCurrencyName();
		$("workerProductivity["+i+"]").innerHTML = workers[i].getProductivity();
		$("workerUnitsProduced["+i+"]").innerHTML = roundTo2(workers[i].getProductivity()/$("rawRequired").getInnerText() );
	}

	$("totalSales").innerHTML = company.getSales()+ " " + getCurrencyName();
	
	$("unitsProduced").innerHTML = company.getUnitsProduced();
	if (!isRaw()) {
		$("totalRawCost").innerHTML = roundTo2(company.getRawUsed() * company.getRawPrice()) + " " + getCurrencyName();
		$("rawUsed").innerHTML = company.getRawUsed();
	}
	$("salariesCost").innerHTML = company.getTotalSalaries() + " " + getCurrencyName();
	$("allCosts").innerHTML = roundTo2(company.getTotalSalaries() + company.getRawUsed() * company.getRawPrice()) + " " + getCurrencyName();	


	if (company.getProfit()<=0) {
		$("profit").setAttribute("class", "red");
		$("profitInGold").setAttribute("class", "red");
	}
	else {
		$("profit").setAttribute("class", "green");
		$("profitInGold").setAttribute("class", "green");
	}
	$("profit").innerHTML = company.getProfit() + " " + getCurrencyName();
	$("profitInGold").innerHTML = roundTo2(company.getProfit() * $("currencyValue").value ) ;
	$("boostersCost").innerHTML = company.getBoostersCost() + " " + getCurrencyName();
	$("productPriceWithTaxes").innerHTML = roundTo2( $("productPrice").value * ( (100 + roundTo2($("vat").getInnerText()) )/100.0) ) + " " + getCurrencyName();
}

function roundTo2(value) {
	return Math.round(value*100.0)/100.0;
}

function calculateProfit(company, worker) {
		
	if (worker.getDisabled()) {
		worker.setProductivity(0);
		worker.setProfit(0);
		return 0;
	}
	var WnM=getWorkerNumberMultiplier(company); // worker number multiplier
	var RM=getRawMultiplayer(company);
	var SM=getSkillMultiplier(worker);
	var BM=getBoosterMultiplier(worker);
	var WM=getWellnessMultiplier(worker);
	
	var productivity = Math.floor( WnM * RM * SM * BM * WM * 8 );
	worker.setProductivity(productivity);
	
	var producedUnits = productivity/company.getRawRequired();
	var rawCost = company.getRawPrice()*company.getRawRequired() * producedUnits;
	var sales = company.getProductPrice() * producedUnits;
	var booster = worker.getBoosterCost();
	var profit = -worker.getSalary() -rawCost -booster + sales;
	worker.setProfit( roundTo2(profit) );
	
	return profit;
}

function getCurrencyName() {
	return $("currencyName").getInnerText();
}

function getCompanyFromForm() {
	var company = new Company();
	company.setWorkers( getWorkersFromForm() );	
	if (isRaw())
		company.setRawPrice( 0 );
	else
		company.setRawPrice( $("rawPrice").value );
	company.setRawRequired( $("rawRequired").getInnerText() );
	company.setProductPrice( $("productPrice").value );
	company.setCurrencyValue( $("currencyValue").value );
	return company;
}


function recalculate() {
	var company = getCompanyFromForm();
	var profit = 0;
	var totalProductivity = 0;
	var totalSalaries = 0;
	var boostersCost = 0;
	for (i=0; i < company.getWorkers().length; i++) {
		var worker = company.getWorkers()[i];
		profit += 1 * calculateProfit(company, worker);
		totalProductivity += 1* roundTo2(worker.getProductivity());
		totalSalaries = totalSalaries + 1 * worker.getSalary();
		boostersCost = boostersCost + worker.getBoosterCost();

	}

	company.setUnitsProduced( roundTo2(totalProductivity/company.getRawRequired()));
	company.setProfit( roundTo2(profit) );
	company.setSales( roundTo2(totalProductivity/company.getRawRequired()*company.getProductPrice() ) );
	company.setRawUsed(totalProductivity);
	company.setTotalSalaries( roundTo2(totalSalaries) );
	company.setBoostersCost( roundTo2(boostersCost) );
	updateGUI(company);
	return company;
}


</script>


<table width="100%">
	<tr>
	
	
		<td><center>

<table width="850" border="1" style="font-size: 12px" cellspacing="1">
		<tr>
			<td>
			
				<table width="100%">
					<tr><td>
					<center><h3>Company details</h3></center>

						<table width="100%">
							<tr>
							
								<td>
									<table class="searchTable">					
										<tr><td>Type: </td> <td id="productTypeValue"> <c:out value="${ company.companyType.name }"/></td> </tr>
										<tr><td>Quality: </td> <td> Q<c:out value="${ company.quality }"/></td> </tr>
										<tr><td>Location: </td> <td> <c:out value="${ company.company.country.name }"/>
													<img src="http://www.erepublik.com/images/flags/M/<c:out value="${company.company.country.urlName}"/>.gif"/>
										
										</td> </tr>
										<tr><td>Name: </td> <td> 
											<a target="_blank" href="http://economy.erepublik.com/en/company/moooo/<c:out value="${company.company.id}"/>">
												<c:out value="${company.company.name}"/>
											</a>
										</td> </tr>
										<tr><td>ID: </td> <td> <c:out value="${company.company.id}"/></td> </tr>
									</table>	
								</td>
									
								
								<td>
									<center>
										<c:if test="${company.companyType.raw == true}">
											<img width="90" src="http://www.erepublik.com/images/icons/industry/<c:out value="${company.companyType.erepublikId}"/>/default.png"/>
										</c:if>
										<c:if test="${company.companyType.raw != true}">
											<img width="90" src="http://www.erepublik.com/images/icons/industry/<c:out value="${company.companyType.erepublikId}"/>/q<c:out value="${company.quality}"/>.png"/><br/>			
										</c:if>		
									</center>
								</td>
							
							</tr>
						</table>
								
					</td></tr>
					<tr><td>
						<center><h3>Workers</h3>
						<table  style="text-align: center;" width="100%">
						<tbody id="workersTable">
							<tr style="font-weight: bold;">
								<td> </td> 
								<td id="workers_header">name</td> 
								<td>skill</td> 
								<td>wellness</td> 
								<td>booster</td> 
								<td>productivity</td> 
								<td>units produced</td> 
								<td>salary</td> 
								<td>profit</td>
								<td>disable<br/>worker</td>
								<td>del.</td>
							</tr>
							
							<c:forEach var="worker" items="${company.workers}" varStatus="loop" >
								<tr id="worker[<c:out value="${loop.index}"/>]" >
									<td id="workerID[<c:out value="${loop.index}"/>]">  <c:out value="${loop.index+1}"/> </td>
									<td>
										<a href="http://www.erepublik.com/en/citizen/profile/<c:out value="${worker.id}"/>" target="_blank">
											<c:out value="${worker.name}"/>
										</a>
									</td> 
									<td>
										<input id="workerSkill[<c:out value="${loop.index}"/>]" type="text" size="2" value="<c:out value="${worker.skill}"/>"/>
									</td> 
									<td>
										<input id="workerWellness[<c:out value="${loop.index}"/>]" type="text" size="4" value="<c:out value="${worker.wellness}"/>"/>
									</td> 
									
									<td>
										<select id="workerBooster[<c:out value="${loop.index}"/>]">
											<option value="1.1">+10%(0g)</option>
											<option value="1.5">+50%(0.19g)</option>
											<option value="2.0">+100%(0.79g)</option>
											<option value="3.0">+200%(0.99g)</option>
										</select>
									</td> 
									
									<td id="workerProductivity[<c:out value="${loop.index}"/>]"><c:out value="${worker.productivity}"/></td> 
									<td id="workerUnitsProduced[<c:out value="${loop.index}"/>]"><c:out value="${worker.unitsProduced}"/></td> 
									<td> <input id="workerSalary[<c:out value="${loop.index}"/>]" type="text" size="4" value="<c:out value="${worker.salary}"/>" /> <c:out value="${company.company.country.currencyName}"/> </td> 
									<td id="workerProfit[<c:out value="${loop.index}"/>]"
										<c:if test="${worker.profit > 0}">
											class="green"
										</c:if>
										<c:if test="${worker.profit <= 0}">
											class="red"
										</c:if>		
									><c:out value="${worker.profit}"/> <c:out value="${company.company.country.currencyName}"/> </td>
									<td>
										<input id="workerDisable[<c:out value="${loop.index}"/>]" type="checkbox" />
									</td> 
									
									<td>
										<a id="workerDelete[<c:out value="${loop.index}"/>]" href="#" onmousedown="deleteWorker(<c:out value="${loop.index}"/>);">
											<img src="http://campus.codechef.com/images/cross-icon.gif" border=0/>
										</a>
									</td>
									
								</tr>
							</c:forEach>
							 
						</table>
						
						<input type="submit" value="Add new worker" onmousedown="addWorker();" />
						<br/>
						
						</center>
					</td></tr>
					</tbody>
				</table>
			
			</td>
			
			<td  valign="top">
				<center>
				<table>
					<tr><td>
					
						<table>
						<tr><td>Currency name:</td> <td  id="currencyName"><c:out value="${company.company.country.currencyName}"/></td> </tr>
						<tr><td>Currency value:</td> <td> 1  <c:out value="${company.company.country.currencyName}"/> = 
								<input type="text" size="5" id="currencyValue" value="<c:out value="${currency}"/>"/> <img src="http://34all.org/erep/gold.gif"/></td> </tr>
						<tr><td>Vat(%):</td> <td id="vat"><c:out value="${company.vat}"/></td> </tr>
						<tr><td>Raw region:</td> <td id="rawRegionLevel"><c:if test="${company.companyType.raw == true}"><c:out value="${company.rawRegion.name}"/></c:if><c:if test="${company.companyType.raw == false}">n/a</c:if></td> </tr>
						<tr><td>Productivity per unit:</td> <td id="rawRequired"><c:out value="${company.companyType.rawRequired * company.quality}"/></td> </tr>
						
						
						</table>
						<center><h3>Daily revenue</h3></center>
						
						<table>
							<tr><td>Units produced:</td> <td id="unitsProduced"><c:out value="${company.dailyProduction}"/>
							

							</td> </tr>
							<tr><td>Price per unit without VAT:</td> <td> <input id="productPrice" type="text" size="8" value="<c:out value="${company.pricePerUnitWithoutTaxes}"/>"/> 
									<c:out value="${company.company.country.currencyName}"/></td> </tr>
							<tr><td>Price per unit with VAT:</td> <td id="productPriceWithTaxes"> <c:out value="${company.pricePerUnitWithTaxes}"/> <c:out value="${company.company.country.currencyName}"/></td> </tr>
							<tr><td>Total revenue:</td> <td class="green" id="totalSales"> <c:out value="${company.dailyRevenue}"/> <c:out value="${company.company.country.currencyName}"/></td> </tr>
						</table>
					</td></tr>
					<tr><td>
						<center><h3>Daily cost</h3></center>
						<table>
							<tr><td >Raw used:</td> 
							<td id="rawUsed"> 
									<c:if test="${company.companyType.raw == true}">
										n/a
									</c:if>
									<c:if test="${company.companyType.raw == false}">
										<c:out value="${company.rawUsed}"/>
									</c:if>
							</td></tr>
							<tr><td>Raw cost:</td> <td> <input id="rawPrice" type="text" size="5" value="<c:if test="${company.companyType.raw == true}">n/a</c:if><c:if test="${company.companyType.raw == false}"><c:out value="${company.rawCost}"/></c:if>"/>
									 <c:out value="${company.company.country.currencyName}"/>  </td> </tr>
							<tr><td>Total raw cost:</td> <td class="red" id="totalRawCost"> 
							
									<c:if test="${company.companyType.raw == true}">
										n/a
									</c:if>
									<c:if test="${company.companyType.raw == false}">
										<c:out value="${company.totalRawPrice}"/>
										<c:out value="${company.company.country.currencyName}"/>
									</c:if>
							
							  </td> </tr>

							<tr><td>Salaries cost:</td> <td class="red" id="salariesCost"> <c:out value="${company.dailySalaries}"/> <c:out value="${company.company.country.currencyName}"/> </td> </tr>
							<tr><td>Boosters cost:</td> <td class="red" id="boostersCost"> 0 <c:out value="${company.company.country.currencyName}"/> </td> </tr>
							
							<tr><td>All costs:</td> <td class="red" id="allCosts"> 
										<c:set var="allCosts"><fmt:formatNumber value="${company.totalRawPrice+company.dailySalaries}" maxFractionDigits="2"/></c:set>
										<c:out value="${allCosts}"/>	<c:out value="${company.company.country.currencyName}"/>	
							  </td> </tr>
							
						</table>		
					</td></tr>
					<tr><td>
					<center><h3>Profit</h3></center>
					<table>
					
						<c:set var="profitInGold"><fmt:formatNumber value="${company.dailyProfit * currency}" maxFractionDigits="2"/></c:set>
					
						<tr><td><b>Daily profit</b>:</td> <td id="profit"
						<c:if test="${company.dailyProfit > 0}">
							class="green"
						</c:if>
						<c:if test="${company.dailyProfit <= 0}">
							class="red"
						</c:if>						
						 ><c:out value="${company.dailyProfit}"/> <c:out value="${company.company.country.currencyName}"/> </td> </tr>
						 
						 	<tr><td><b> Profit in <img src="http://34all.org/erep/gold.gif"/> </b></td> <td id="profitInGold"
							<c:if test="${company.dailyProfit > 0}">
								class="green"
							</c:if>
							<c:if test="${company.dailyProfit <= 0}">
								class="red"
							</c:if>						
						 ><c:out value="${profitInGold}"/>
						 
						 </td> </tr>
						 
					</table>
					</td></tr>
				</table>
	
				<center>
					
					<input type="submit" value="Recalculate!" onmousedown="recalculate();" /></center>
	
				</center>
			</td>
			
		</tr>
	</table>
	</center>
	</td>
		<td width="120">
		
		<script type="text/javascript"><!--
google_ad_client = "pub-7561623930514913";
/* 120x600, utworzono 10-10-14 */
google_ad_slot = "2660352618";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
		
		
		</td>
		
	</tr>
</table>	
	
	